/**
 @author MAMAMA
 @date 2020-07-12
 @description 布瑞特角度编码器
*/

#ifndef _BRITER_H_
#define _BRITER_H_

#include "Stream.h"
#include "stm32f10x_it.h"
#include "ModbusMaster.h"

#pragma pack(push)     /* save origin value */
#pragma pack(1)


#pragma pack(pop)  /* restore origin value */

#define BRT_RESOLUTION   4096.0f  //分辨率
#define BRT_MAX_TURN     50       //最大圈数

#define BRT_SINGLE_PULSE 0x0003   //编码器单圈值
#define BRT_TURNS        0x0002   //编码器圈数
#define BRT_PULSES       0x0000   //编码器码值

class Briter
{
private:
    ModbusMaster* _pmodbus_master;
    Stream* _pstream;

    enum ConnectState
    {
        OFFLINE = 0,
        ONLINE
    }_connect_state;
    
    uint8_t status;
    
public:
    uint16_t turns;
    uint16_t single_turn;

    /**
    * @brief 获取带小数的圈数
    */
    double getFloatTurn()
    {
        double _turn = turns + single_turn / BRT_RESOLUTION;
        return _turn;
    }
    
public:
    Briter(uint8_t axisid, Stream* pstream);
    ~Briter(void);    
    uint8_t axis_id;          //device address in modbus rtu protocol
    bool initModbus(void);
    bool isOnline(void)
    {
        return (_connect_state != OFFLINE);
    }
    bool getTurns(void);      //获取编码器圈数
    void clear(void);         //重置清零
    bool connectOnce(void);

};

#endif
